import moment from 'moment'
// 获取当前周的开始结束时间
export const currentWeek = function() {
  let data = new Date() //当前日期
  let year = Number(data.getFullYear());//当前年
  let month = Number(data.getMonth())+1;
  let nowMonth =Number(data.getMonth()); //当前月
  let day = Number(data.getDate()); //当天
  //计算某日是本月第几周
  let mydate=new Date(year,month-1,day);
  let weekday=mydate.getDay(); //获取该日是星期几，0代表星期日//今天本周的第几天
  let weekno=Math.ceil((day + 6 - weekday) / 7); // 本月第几周
  //获取当前周的开始结束时间
  let startTime //本周的开始时间
  let endTime //本周的结束时间
  function formatDate(date) {
    let myyear = Number(date.getFullYear());
    let mymonth = Number(date.getMonth()+1);
    let myweekday = Number( date.getDate());
    if(mymonth < 10){
      mymonth = "0" + mymonth;
    }
    if(myweekday < 10){
      myweekday = "0" + myweekday;
    }
    return (myyear+"-"+mymonth + "-" + myweekday);
  }
  //获得本周的开始日期
  function getWeekStartDate() {
    let weekStartDate = new Date(year, nowMonth, day + 1 - weekday);
    return formatDate(weekStartDate);
  }
  //获得本周的结束日期
  function getWeekEndDate() {
    let weekEndDate = new Date(year, nowMonth, day + 7 - weekday);
    return formatDate(weekEndDate);
  }
  startTime=getWeekStartDate()
  endTime=getWeekEndDate()
  return {
    weekStartTime: startTime,
    weekEndTime: endTime
  }
}

// 获取当前月的开始结束时间
export function getCurrMonthDays() {
  let obj = {
    monthstarttime: '',
    monthendtime: ''
  }
  obj.monthstarttime = moment(moment().month(moment().month()).startOf('month').valueOf()).format('YYYY-MM-DD')
  obj.monthendtime = moment(moment().month(moment().month()).endOf('month').valueOf()).format('YYYY-MM-DD')
  return obj
}

// 获取当前日期
export function getToday() {
  let obj = {
    daystarttime: ''
  }
  obj.daystarttime = moment(moment().startOf("day").valueOf()).format("YYYY-MM-DD")
  return obj
}

// 中国标准时间format yyyy-mm-dd
const format = (time) => {
  let ymd = ''
  let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
  let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
  ymd += time.getFullYear() + '-' // 获取年份。
  ymd += mouth + '-' // 获取月份。
  ymd += day // 获取日。
  return ymd // 返回日期。
 }
 
 export const getAllDate = (start, end) => {
  let dateArr = []
  let startArr = start.split('-')
  let endArr = end.split('-')
  let db = new Date()
  db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
  let de = new Date()
  de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
  let unixDb = db.getTime()
  let unixDe = de.getTime()
  let stamp
  const oneDay = 24 * 60 * 60 * 1000;
  for (stamp = unixDb; stamp <= unixDe;) {
    dateArr.push(format(new Date(parseInt(stamp))))
    stamp = stamp + oneDay
  }
  return dateArr
 }
 
 